package com.renfei.multimediatask.ui.task1;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.renfei.multimediatask.R;
import com.renfei.multimediatask.base.BaseActivity;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DrawImageActivity extends BaseActivity implements SurfaceHolder.Callback {

    @BindView(R.id.customImageView)
    CustomImageView customImageView;
    @BindView(R.id.surfaceView)
    SurfaceView surfaceView;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_draw_image;
    }

    @Override
    protected void initListener() {


        surfaceView.getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        drawTask task = new drawTask(new WeakReference<>(DrawImageActivity.this));
        task.execute();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
    }


    class drawTask extends AsyncTask<Void, Void, Integer> {
        private WeakReference<DrawImageActivity> weakReference;

        public drawTask(WeakReference<DrawImageActivity> weakReference) {
            this.weakReference = weakReference;
        }

        @Override
        protected Integer doInBackground(Void... voids) {
            Canvas canvas = weakReference.get().surfaceView.getHolder().lockCanvas();
            Bitmap bitmap = BitmapFactory.decodeResource(weakReference.get().getResources(), R.mipmap.login_logo);
            Paint paint = new Paint();
            canvas.drawBitmap(bitmap, 0, 0, paint);

            weakReference.get().surfaceView.getHolder().unlockCanvasAndPost(canvas);
            return 0;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }


    /**
     * 图片的尺寸和屏幕的尺寸不一样，需要把图片调整居中
     **/
    private Rect getBitmapRect(Bitmap bimap) {
        int bimapHeight = bimap.getHeight();
        int bimapWidth = bimap.getWidth();
        int viewWidth = surfaceView.getWidth();
        int viewHeight = surfaceView.getHeight();
        float bimapRatio = (float) bimapWidth / (float) bimapHeight; // 宽高比
        float screenRatio = (float) viewWidth / (float) viewHeight;
        int factWidth;
        int factHeight;
        int x1, y1, x2, y2;
        if (bimapRatio > screenRatio) {
            factWidth = viewWidth;
            factHeight = (int) (factWidth / bimapRatio);
            x1 = 0;
            y1 = (viewHeight - factHeight) / 2;
        } else if (bimapRatio < screenRatio) {
            factHeight = viewHeight;
            factWidth = (int) (factHeight * bimapRatio);
            x1 = (viewWidth - factWidth) / 2;
            y1 = 0;
        } else {
            factWidth = bimapWidth;
            factHeight = bimapHeight;
            x1 = 0;
            y1 = 0;
        }
        x2 = x1 + factWidth;
        y2 = y1 + factHeight;
        return new Rect(x1, y1, x2, y2);
    }
}
